/**
 * 
 */
package com.swim.natatoriumapply.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.swim.common.utils.SmsUtil;
import com.swim.natatoriumapply.entity.NatatoriumApply;
import com.swim.natatoriumapply.service.NatatoriumApplyService;
import com.swim.natatoriuminfo.entity.NatatoriumInfo;
import com.swim.natatoriuminfo.service.NatatoriumInfoService;
import com.swim.natatoriuminfotouser.entity.NatatoriumInfoToUser;
import com.swim.natatoriuminfotouser.service.NatatoriumInfoToUserService;
import com.swim.sys.entity.SysUserRole;
import com.swim.sys.service.SysUserRoleService;

/**
 * 游泳馆申请Controller
 * @author 刘军武
 * @version 2021-01-26
 */
@Controller
@Transactional(rollbackFor = Exception.class)
@RequestMapping(value = "${adminPath}/natatoriumapply/natatoriumApply")
public class NatatoriumApplyController extends BaseController {

	@Autowired
	private NatatoriumApplyService natatoriumApplyService;
	
	@Autowired
	private NatatoriumInfoService natatoriumInfoService;
	
	@Autowired
	private SysUserRoleService userRoleService;
	
	@Autowired
	private NatatoriumInfoToUserService natatoriumInfoToUserService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public NatatoriumApply get(String id, boolean isNewRecord) {
		return natatoriumApplyService.get(id, isNewRecord);
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("natatoriumapply:natatoriumApply:view")
	@RequestMapping(value = {"list", ""})
	public String list(NatatoriumApply natatoriumApply, Model model) {
		model.addAttribute("natatoriumApply", natatoriumApply);
		return "swim/natatoriumapply/natatoriumApplyList";
	}

	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("natatoriumapply:natatoriumApply:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<NatatoriumApply> listData(NatatoriumApply natatoriumApply, HttpServletRequest request, HttpServletResponse response) {
		Page<NatatoriumApply> page = natatoriumApplyService.findPage(new Page<NatatoriumApply>(request, response), natatoriumApply);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("natatoriumapply:natatoriumApply:view")
	@RequestMapping(value = "form")
	public String form(NatatoriumApply natatoriumApply, Model model) {
		model.addAttribute("natatoriumApply", natatoriumApply);
		return "swim/natatoriumapply/natatoriumApplyForm";
	}

	/**
	 * 保存游泳馆申请
	 */
	@RequiresPermissions("natatoriumapply:natatoriumApply:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated NatatoriumApply natatoriumApply, HttpServletRequest request) {
		natatoriumApplyService.save(natatoriumApply);
		try {
			//游泳馆申请入驻提交成功后发送短信给用户
			SmsUtil.sendNatatoriumApply(natatoriumApply.getContactWay(), natatoriumApply.getName());
			//游泳馆申请入驻发送短信给李教练
			SmsUtil.sendNatatoriumApplyByLi("13912785198", natatoriumApply.getName());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return renderResult(Global.TRUE, text("保存游泳馆申请成功！"));
	}

	/**
	 * 查看详情表单
	 */
	@RequiresPermissions("natatoriumapply:natatoriumApply:view")
	@RequestMapping(value = "details")
	public String details(NatatoriumApply natatoriumApply, Model model) {
		model.addAttribute("natatoriumApply", natatoriumApply);
		return "swim/natatoriumapply/natatoriumApplyDetails";
	}

	/**
	 * 审核游泳馆申请
	 */
	@RequiresPermissions("natatoriumapply:natatoriumApply:edit")
	@PostMapping(value = "check")
	@ResponseBody
	public String check(@RequestBody NatatoriumApply natatoriumApply) {
		if (natatoriumApply.getApplyState() == 2) {//同意
			NatatoriumApply natatoriumApplyOne = natatoriumApplyService.get(natatoriumApply.getId());
			//新增游泳馆信息
			NatatoriumInfo natatoriumInfo = new NatatoriumInfo();
			natatoriumInfo.setBusinessStatus(1L);//营业状态 1表示营业中 2表示暂停营业
			natatoriumInfo.setCollaborateStatus(1L);//合作状态 1表示未合作  2表示已合作
			natatoriumInfo.setPlacingFunctionStatus(2L);;		// 下单功能状态 1表示开启  2表示关闭
			natatoriumInfo.setShowNavigationStatus(1L);		// 导航功能状态 1表示开启  2表示关闭        
			natatoriumInfo.setShowMapStatus(1L);		// 地图功能状态 1表示开启  2表示关闭       
			natatoriumInfo.setShowBookingStatus(2L);		// 在线预约功能状态 1表示开启  2表示关闭
			natatoriumInfo.setConceptionStatus(2L);		// 教练风采展示状态 1表示开启  2表示关闭
			natatoriumInfo.setWorkShopStatus(2L);		// 体验课展示状态 1表示开启  2表示关闭
			natatoriumInfo.setName(natatoriumApplyOne.getName());		// 场馆名称          
			natatoriumInfo.setRanking(1000L);;//排名默认1000
			natatoriumInfo.setApplyTime(natatoriumApplyOne.getCreateDate());		// 申请时间             
			natatoriumInfo.setAddress(natatoriumApplyOne.getAddress());		// 场馆地址      
			natatoriumInfo.setCity(natatoriumApplyOne.getCity());		// 城市
			natatoriumInfo.setArea(natatoriumApplyOne.getArea());		// 区县
			natatoriumInfo.setPrincipal(natatoriumApplyOne.getContactPerson());		// 负责人          
			natatoriumInfo.setVenueDescription(natatoriumApplyOne.getVenueDescription());		// 场馆描述 
			natatoriumInfo.setPhone(natatoriumApplyOne.getContactWay());		// 联系电话             
			natatoriumInfoService.save(natatoriumInfo);
//			//新增用户
//			User u = new User();
//			u.setIsNewRecord(true);
//			u.setLoginCode(natatoriumApplyOne.getContactWay());
//			u.setUserName(natatoriumApplyOne.getContactPerson());
//			u.setPassword("123456");
//			u.setMobile(natatoriumApplyOne.getContactWay());
//			u.setUserType("employee");
//			u.setMgrType(User.MGR_TYPE_NOT_ADMIN);
//			u.setRefCode(natatoriumApplyOne.getContactWay());
//			u.setRefName(natatoriumApplyOne.getContactPerson());
//			userService.save(u);
//			//新增员工（jeesite中的员工表）
//			Employee employee = new Employee();
//			employee.setEmpCode(u.getUserCode());
//			employee.setEmpName(natatoriumApplyOne.getContactPerson());
//			Office office = new Office();
//			office.setOfficeCode("natatorium");
//			office.setOfficeName("游泳馆");
//			employee.setOffice(office);
//			employee.setCorpCode("0");
//			employee.setCorpName("JeeSite");
//			employeeDao.insert(employee);
//			//新增游泳馆员工(t_employee)
//			EmployeeSwim employeeSwim = new EmployeeSwim();
//			employeeSwim.setNatatoriumInfoId(natatoriumInfo.getId());		// 游泳馆信息id 
//			employeeSwim.setName(natatoriumApplyOne.getContactPerson());		// 员工姓名                
//			employeeSwim.setRole("natatoriumAdmin");		// 角色                  
//			employeeSwim.setUserCode(natatoriumApplyOne.getContactWay());		// 登录账号            
//			employeeSwimService.save(employeeSwim);
			//新增用户角色关系数据
			SysUserRole sysUserRole = new SysUserRole();
			sysUserRole.setUserCode(natatoriumApplyOne.getCreateBy());
			sysUserRole.setRoleCode("user");//用户 角色
			userRoleService.deleteUserRole(sysUserRole);//删除用户角色关系数据（学员角色）
			sysUserRole.setRoleCode("natatoriumAdmin");//游泳馆管理员 角色
			userRoleService.insertUserRole(sysUserRole);
			//新增用户游泳馆信息关联数据
			NatatoriumInfoToUser natatoriumInfoToUser = new NatatoriumInfoToUser();
			natatoriumInfoToUser.setUserCode(natatoriumApplyOne.getCreateBy());
			natatoriumInfoToUser.setNatatoriumInfoId(natatoriumInfo.getId());
			natatoriumInfoToUserService.save(natatoriumInfoToUser);
		}
		natatoriumApplyService.update(natatoriumApply);
		return renderResult(Global.TRUE, text("审核成功！"));
	}

}